package com.lingxu.api.ztb.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingxu.api.ztb.model.BiaoduanBidderPair;
import com.lingxu.api.ztb.model.vo.*;
import com.lingxu.api.ztb.service.*;
import com.lingxu.base.common.api.vo.Result;
import com.lingxu.base.system.common.entity.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: ptmh
 * @ClassName ZTBBaseController
 * @description:
 * @author: gxy
 * @create: 2025-07-18 16:40
 * @Version 1.0
 **/
@Slf4j
@Api(tags = "招投标信息管理", value = "ZTBBaseController")
@Validated
@CrossOrigin
@RestController
@RequestMapping("/projectbase")
public class ZTBBaseController {

    @Autowired
    private IZtbParagraphService ztbParagraphService;

    @Autowired
    private IZtbBiddingnoticeService biddingnoticeService;

    @Autowired
    private IZtbTenderapplyService tenderapplyService;

    @Autowired
    private IZtbTrialresultService trialresultService;

    @Autowired
    private IZtbBidwinshowService bidwinshowService;

    @Autowired
    private IZtbContractService ztbContractService;

    @Autowired
    private IZtbTenderapplyprivatemessageService ztbTenderapplyprivatemessageService;

    @Autowired
    private IZtbProjectService iZtbProjectService;

    @Autowired IZtbBiddingcandidateService iZtbBiddingcandidateService;

    @Autowired
    private IViewRegCompanyService  viewRegCompanyService;


    @ApiOperation(value="获取标段列表用于下拉")
    @PostMapping(value = "/listParagraph")
    public Result<?> listParagraph(@RequestBody ZtbParagraphVO query) {
        return Result.ok(ztbParagraphService.list(query));
    }

    @ApiOperation(value="获取标段信息详情")
    @PostMapping(value = "/getParagraphInfo")
    public Result<?> getParagraphInfo(@RequestBody ZtbParagraphVO query) {
        return Result.ok(ztbParagraphService.getZtbParagraph(query));
    }

    @ApiOperation(value="获取最新一条招标公告信息")
    @PostMapping(value = "/getZtbBiddingnotice")
    public Result<?> getZtbBiddingnotice(@RequestBody ZtbParagraphVO query) {
        return Result.ok(biddingnoticeService.getZtbBiddingnotice(query));
    }

    @ApiOperation(value="根据招标公告获取投标信息列表")
    @PostMapping(value = "/listZtbBiddingnotice")
    public Result<?> listZtbBiddingnotice(@RequestBody ZtbBiddingnoticeVO query) {
        return Result.ok(tenderapplyService.listZtbBiddingnotice(query));
    }

    @ApiOperation(value="根据招标公告获取评标信息列表")
    @PostMapping(value = "/listZtbTrialresult")
    public Result<?> listZtbTrialresult(@RequestBody ZtbBiddingnoticeVO query) {
        return Result.ok(trialresultService.listZtbTrialresult(query));
    }

    @ApiOperation(value="根据招标公告获取中标结果")
    @PostMapping(value = "/getZtbBidwinshow")
    public Result<?> getZtbBidwinshow(@RequestBody ZtbBiddingnoticeVO query) {
        return Result.ok(bidwinshowService.getZtbBidwinshow(query));
    }

    @ApiOperation(value="根据项目编号获取所有标段下最新一条招标公告信息")
    @PostMapping(value = "/listBiddingnotice")
    public Result<?> listBiddingnotice(@RequestBody ZtbParagraphVO query) {
        String projectId = iZtbProjectService.getZtbProjectIdByProjectName(query.getProjectname());
        query.setProjectid(projectId);
        Page<ZtbBiddingnoticeVO> page = new Page<ZtbBiddingnoticeVO>(query.getPageNo(), query.getPageSize());
        IPage<ZtbBiddingnoticeVO> result=biddingnoticeService.listBiddingnotice(page,query);
        return Result.ok(result);
    }

    @ApiOperation(value = "根据项目编号获取所有标段下的投标信息")
    @PostMapping(value = "/listZtbTenderapply")
    public Result<?> listZtbTenderapply(@RequestBody ZtbBiddingnoticeVO query) {
        String projectId = iZtbProjectService.getZtbProjectIdByProjectName(query.getProjectname());
        query.setProjectid(projectId);
        Page<ZtbTenderapplyVO> page = new Page<ZtbTenderapplyVO>(query.getPageNo(), query.getPageSize());
        IPage<ZtbTenderapplyVO> tenderapply = tenderapplyService.listZtbTenderapply(page, query);
        tenderapply.getRecords().forEach(item->{
            String biaoduanid = item.getBiaoduanid();
            //根据标段id 查询 投标人单位ID
            List<String> companieds = ztbTenderapplyprivatemessageService.CompanyIdByParagraphId(biaoduanid);
            companieds.forEach(companied->{
                //根据投标单位id查询 企业名称NAME
                String companyName = viewRegCompanyService.getCompanyNameByCompanied(companied);
                item.setTbcompany(companyName);
            });
        });
        return Result.ok(tenderapply);
    }

    @ApiOperation(value = "根据项目编号获取所有标段下的评标信息")
    @PostMapping(value = "/ztblistZtbTrialresult")
    public Result<?> ztblistZtbTrialresult(@RequestBody ZtbBiddingnoticeVO query) {
        String projectId = iZtbProjectService.getZtbProjectIdByProjectName(query.getProjectname());
        query.setProjectid(projectId);
        Page<ZtbTrialresultVO> page = new Page<ZtbTrialresultVO>(query.getPageNo(), query.getPageSize());
        return Result.ok(trialresultService.ztblistZtbTrialresult(page,query));
    }
   @ApiOperation(value = "根据项目编号获取所有标段下的中标结果信息")
    @PostMapping(value = "/listZtbBidwinshow")
    public Result<?> listZtbBidwinshow(@RequestBody ZtbBiddingnoticeVO query) {
       String projectId = iZtbProjectService.getZtbProjectIdByProjectName(query.getProjectname());
       query.setProjectid(projectId);
       Page<ZtbBidwinshowVO> page = new Page<ZtbBidwinshowVO>(query.getPageNo(), query.getPageSize());
       Page<ZtbBidwinshowVO> listZtbBidwinshow = bidwinshowService.listZtbBidwinshow(page, query);
       listZtbBidwinshow.getRecords().forEach(item->{
           //中标单位
           String biddercompanyname = item.getBiddercompanyname();
           String biaoduanid = item.getBiaoduanid();
           String hxr = iZtbBiddingcandidateService.isHxr(biaoduanid,biddercompanyname);
           item.setIscandidate(hxr);
       });
       return Result.ok(listZtbBidwinshow);
    }

    @ApiOperation(value = "查询合同信息")
    @PostMapping(value = "/listContract")
    public Result<?> listContract(@RequestBody ZtbBiddingnoticeVO query) {
        // 获取项目ID
        String projectId = iZtbProjectService.getZtbProjectIdByProjectName(query.getProjectname());
        query.setProjectid(projectId);

        Page<ZtbBidwinshowVO> bidwinPage = new Page<>(query.getPageNo(), query.getPageSize());
        Page<ZtbBidwinshowVO> listZtbBidwinshow = bidwinshowService.listZtbBidwinshow(bidwinPage, query);

        List<String> biaoduanIds = listZtbBidwinshow.getRecords().stream()
                .map(ZtbBidwinshowVO::getBiaoduanid)
                .distinct()
                .collect(Collectors.toList());
        ZtbContractVO contractQuery = new ZtbContractVO();
        contractQuery.setParagraphids(biaoduanIds);
        contractQuery.setPageNo(query.getPageNo());
        contractQuery.setPageSize(query.getPageSize());
        // 查询合同信息
        Page<ZtbContractVO> contractPage = ztbContractService.queryPageList(contractQuery);
        contractPage.setTotal(listZtbBidwinshow.getTotal());

        return Result.ok(contractPage);
    }

    /**
     * 招投标信息
     *
     * @param query
     * @return {@link Result }<{@link ? }>
     */
    @PostMapping(value = "/listZtbProject")
    public Result<?> listZtbProject(@RequestBody ZtbParagraphVO query){
        //标段名称、招标单位、招标代理单位
        String projectId = iZtbProjectService.getZtbProjectIdByProjectName(query.getProjectname());
        query.setProjectid(projectId);

        Page<ZtbBiddingnoticeVO> page = new Page<ZtbBiddingnoticeVO>(query.getPageNo(), query.getPageSize());

        IPage<ZtbBiddingnoticeVO> result = biddingnoticeService.getZTBData(page, query);

        return Result.ok(result);
    }

    @PostMapping(value = "/listZtbProjectNew")
    public Result<?> listZtbProjectNew(@RequestBody ZtbParagraphVO query){

        Page<ZTBProjectInfo> page = new Page<ZTBProjectInfo>(query.getPageNo(), query.getPageSize());

        IPage<ZTBProjectInfo> result = iZtbProjectService.getZtbProjectIdByProjectNameNew(page,query);

        return Result.ok(result);
    }
}
